package com.xianzhiOA.service.impl;/**
 * Created by Administrator on 2017/5/8.
 */

import com.xianzhiOA.bean.XzApprovalProcess;
import com.xianzhiOA.bean.XzTempletBase;
import com.xianzhiOA.bean.XzTempletExtend;
import com.xianzhiOA.dao.XzApprovalProcessMapper;
import com.xianzhiOA.service.XzApprovalProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 审批流程表
 *
 * @author 王诗博
 * @create 2017-05-08 16:56
 **/
@Service("XzApprovalProcessService")
public class XzApprovalProcessServiceImpl implements XzApprovalProcessService {
    @Autowired
    private XzApprovalProcessMapper ApprovalProcessMapper;

    @Override
    public List<XzApprovalProcess> selByTempletBaseId(Long templetBaseId) {
        List<XzApprovalProcess> list=ApprovalProcessMapper.selByTempletBaseId(templetBaseId);

        return list;
    }

    @Override
    public int addApprovalProcess(XzTempletBase xzTempletBase) {
        int i=0;
        List approvalProcessList=xzTempletBase.getNodeList();
        //获取已经添加的节点模板信息的个数
        int oldNodecount=0;
        if(xzTempletBase.getExtendObjectList() != null){
            oldNodecount=xzTempletBase.getExtendObjectList().size();
        }


        int node=0;
        for(int j=0;j<approvalProcessList.size();j++){
            XzApprovalProcess newApprovalProcess=new XzApprovalProcess();
            newApprovalProcess.setTempletBaseId(xzTempletBase.getTempletBaseId());   //模板ID
            if(oldNodecount>0){
                if(node==0) {
                    node = oldNodecount;
                }
                node = node + 1;
            }else {
                node = j + 1;
            }
            newApprovalProcess.setNodeId(node); //节点顺序ID
            Long roleid=Long.parseLong(approvalProcessList.get(j).toString());
            newApprovalProcess.setRoleId(roleid); //角色ID
            i=ApprovalProcessMapper.addApprovalProcess(newApprovalProcess);
            System.out.println("i:"+i);
        }

        return i;
    }
   //删除指定模板编号的数据
    @Override
    public int deleteNodeByKey(XzApprovalProcess xzApprovalProcess) {
        int i=0;
        i=ApprovalProcessMapper.deleteNodeByKey(xzApprovalProcess);
        return i;
    }

  //修改节点表
    @Override
    public int updateNodeByKey(XzApprovalProcess xzApprovalProcess) {
        int i=0;
        i=ApprovalProcessMapper.updateNodeByKey(xzApprovalProcess);
        return i;
    }
   //修改后的添加方法
    @Override
    public int updateOfinsert(XzTempletBase xzTempletBase) {
        int i=0;
        List<XzApprovalProcess> nodeObjectList=xzTempletBase.getNodeObjectList();
        for(int j=0;j<nodeObjectList.size();j++){
            XzApprovalProcess newapprovalProcess=nodeObjectList.get(j);
            int node=j+1;
            newapprovalProcess.setNodeId(node);
            i=ApprovalProcessMapper.addApprovalProcess(newapprovalProcess);
        }
        return i;
    }
    //删除一条扩展模板表中的数据（按id删除）
    @Override
    public int deleteByKey(XzApprovalProcess xzApprovalProcess) {
        int i=0;
        i=ApprovalProcessMapper.deleteByKey(xzApprovalProcess);
        return i;
    }
}
